<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="单词ID" prop="vocabularyId">-->
<!--        <el-input-->
<!--          v-model="queryParams.vocabularyId"-->
<!--          placeholder="请输入单词ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="projectId">-->
<!--        <el-input-->
<!--          v-model="queryParams.projectId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="vocabularyBookId">-->
<!--        <el-input-->
<!--          v-model="queryParams.vocabularyBookId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="简短中文词义" prop="shortMeaning">-->
<!--        <el-input-->
<!--          v-model="queryParams.shortMeaning"-->
<!--          placeholder="请输入简短中文词义"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态" prop="positive">-->
<!--        <el-input-->
<!--          v-model="queryParams.positive"-->
<!--          placeholder="请输入0，消极1，积极2，中性"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="paradigmId">-->
<!--        <el-input-->
<!--          v-model="queryParams.paradigmId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="翻译发音的storeId" prop="translationStoreId">-->
<!--        <el-input-->
<!--          v-model="queryParams.translationStoreId"-->
<!--          placeholder="请输入翻译发音的storeId"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="例句发音的storeId" prop="sentenceStoreId">-->
<!--        <el-input-->
<!--          v-model="queryParams.sentenceStoreId"-->
<!--          placeholder="请输入例句发音的storeId"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vocabulary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vocabulary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:vocabulary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vocabulary:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportAll"
          v-hasPermi="['system:vocabulary:export']"
        >整库导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vocabularyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="单词ID" align="center" prop="vocabularyId" />
      <el-table-column label="词义" align="center" prop="meaning" />
      <el-table-column label="词性" align="center" prop="wordClass" />
      <el-table-column label="配图路径" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="近义词" align="center" prop="synonym" />
<!--      <el-table-column label="${comment}" align="center" prop="projectId" />-->
<!--      <el-table-column label="${comment}" align="center" prop="vocabularyBookId" />-->
      <el-table-column label="简短中文词义" align="center" prop="shortMeaning" />
      <el-table-column label="积极性" align="center" prop="positive" />
<!--      <el-table-column label="${comment}" align="center" prop="paradigmId" />-->
      <el-table-column label="英语解释" align="center" prop="translation" />
      <el-table-column label="翻译发音的storeId" align="center" prop="translationStoreId" />
      <el-table-column label="例句" align="center" prop="sentence" />
      <el-table-column label="例句试听" align="center" prop="sentenceVoice" >
        <template slot-scope="scope">
          <el-button type="text" @click="playAudio(scope.row.id)" icon="el-icon-headset"/>
        </template>
      </el-table-column>
      <el-table-column label="例句中文" align="center" prop="sentenceCn" />
      <el-table-column label="例句发音的storeId" align="center" key="sentenceStoreId" prop="sentenceStoreId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vocabulary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vocabulary:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-audio"
            @click="handleGenAudio(scope.row)"
            v-hasPermi="['system:vocabulary:edit']"
          >重新生成音频</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单词详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单词ID" prop="vocabularyId">
          <el-input v-model="form.vocabularyId" placeholder="请输入单词ID" />
        </el-form-item>
        <el-form-item label="词义" prop="meaning">
          <el-input v-model="form.meaning" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="词性" prop="wordClass">
          <el-input v-model="form.wordClass" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="英语解释" prop="translation">
          <el-input v-model="form.translation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="例句" prop="sentence">
          <el-input v-model="form.sentence" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="配图路径" prop="image">
          <image-upload :limit="1" v-model="form.image"/>
        </el-form-item>
        <el-form-item label="近义词" prop="synonym">
          <el-input v-model="form.synonym" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="isDelete">-->
<!--          <el-input v-model="form.isDelete" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="projectId">-->
<!--          <el-input v-model="form.projectId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="vocabularyBookId">-->
<!--          <el-input v-model="form.vocabularyBookId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="简短中文词义" prop="shortMeaning">
          <el-input v-model="form.shortMeaning" placeholder="请输入简短中文词义" />
        </el-form-item>
        <el-form-item label="积极性" prop="positive">
          <el-input v-model="form.positive" placeholder="请输入0，消极1，积极2，中性" />
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="paradigmId">-->
<!--          <el-input v-model="form.paradigmId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="翻译发音的storeId" prop="translationStoreId">-->
<!--          <el-input v-model="form.translationStoreId" placeholder="请输入翻译发音的storeId" />-->
<!--        </el-form-item>-->
        <el-form-item label="例句中文" prop="sentenceCn">
          <el-input v-model="form.sentenceCn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="例句发音的storeId" prop="sentenceStoreId">-->
<!--          <el-input v-model="form.sentenceStoreId" placeholder="请输入例句发音的storeId" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVocabulary,
  getVocabulary,
  delVocabulary,
  addVocabulary,
  updateVocabulary,
  genAudio, getAudio
} from '@/api/wordmemory/vocabulary_info'

export default {
  name: "VocabularyInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      audio: new Audio(), // 创建一个audio实例

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单词详情表格数据
      vocabularyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vocabularyId: null,
        meaning: null,
        wordClass: null,
        translation: null,
        sentence: null,
        image: null,
        synonym: null,
        isDelete: null,
        projectId: null,
        vocabularyBookId: null,
        shortMeaning: null,
        positive: null,
        paradigmId: null,
        translationStoreId: null,
        sentenceCn: null,
        sentenceStoreId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const vocabularyId = this.$route.params && this.$route.params.id;
    console.log(`vocabularyId is ${vocabularyId}`)
    this.queryParams.vocabularyId = vocabularyId;
    this.getList();
  },
  methods: {
    playAudio(id) {
      getAudio({id}).then(response => {
        console.log(response.data.url)
        this.audio.src = response.data.url; // 设置音频的src属性为点击的音频URL
        this.audio.type = 'audio/mpeg'; // 设置音频的src属性为点击的音频URL
        this.audio.play(); // 播放音频
      });

    },
    /** 查询单词详情列表 */
    getList() {
      this.reset();
      this.loading = true;
      listVocabulary(this.queryParams).then(response => {
        this.vocabularyList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(() => {
          this.$forceUpdate()
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vocabularyId: null,
        meaning: null,
        wordClass: null,
        translation: null,
        sentence: null,
        image: null,
        synonym: null,
        isDelete: null,
        projectId: null,
        vocabularyBookId: null,
        shortMeaning: null,
        positive: null,
        paradigmId: null,
        translationStoreId: null,
        sentenceCn: null,
        sentenceStoreId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单词详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVocabulary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单词详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleGenAudio(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认重新生成单词详情编号为"' + ids + '"的例句发音？').then(function() {
        return genAudio({ ids: ids });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch((e) => {
        console.log(e);
        this.$modal.msgError(e || '操作失败');
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除单词详情编号为"' + ids + '"的数据项？').then(function() {
        return delVocabulary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('word-memory/vocabulary-info/export', {
        ...this.queryParams
      }, `vocabulary_${new Date().getTime()}.xlsx`)
    },

    handleExportAll() {
      this.download('word-memory/vocabulary-info/export', {
        ...this.queryParams,
        vocabularyId: null
      }, `vocabulary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
